package com.UnionPayCenter.controller;

import com.UnionPayCenter.distribution.dto.*;
import com.UnionPayCenter.distribution.service.DistributionOperatorService;
import com.UnionPayCenter.distribution.service.DistributionQueryService;
import com.UnionPayCenter.distribution.vo.*;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.UserLevelEntity;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.IUserLevelService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author Noah
 * @since 1.0.0, 2019/07/01
 */
@Slf4j
@Api(value = "distribution", tags = "分销")
@RestController
@RequestMapping("/distribution")
public class DistributionController {

    @Autowired
    private DistributionQueryService distributionQueryService;

    @Autowired
    private DistributionOperatorService distributionOperatorService;

    @Autowired
    private IUserLevelService userLevelService;

    /**
     * 分销比例查询
     *
     * @param distributionBaseDTO
     * @return
     */
    @CurrentManager
    @RequestMapping(value = "/back/shop/shareprop/list", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[后台]查询分销模板信息", response = DistributionTemplateVO.class)
    public ResponseWrapper listShareProp(
            @ApiParam(value = "分销商户信息") @RequestBody DistributionBaseDTO distributionBaseDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.queryShareProp(distributionBaseDTO));
        } catch (ApiException e) {
            log.error(String.format("[后台]查询分销分润比例失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionBaseDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[后台]查询分销分润比例失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionBaseDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 查询分销分润修改
     *
     * @param distributionSharePropModifyDTO
     * @return
     */
    @RequestMapping(value = "/back/shop/shareprop/update", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[后台]分销分润修改", response = String.class)
    public ResponseWrapper updateShareProp(
            @ApiParam(value = "分销分润比例") @RequestBody DistributionSharePropModifyDTO distributionSharePropModifyDTO) {
        try {
            List<SharePropModifyDTO> modifyDTOList = distributionSharePropModifyDTO.getSharePropModifyDTOList();
            if (CollectionUtils.isEmpty(modifyDTOList)) {
                return null;
            }
            if (null != distributionSharePropModifyDTO.getCounterpartyLevelId()) {
                UserLevelEntity userLevelEntity = userLevelService.selectByLevelId(distributionSharePropModifyDTO.getShopId().intValue(), Integer.valueOf(distributionSharePropModifyDTO.getCounterpartyLevelId()));
                if (Objects.isNull(userLevelEntity)) {
                    throw new ApiException(ErrorCodeEnum.CARE_LEVEL_NOT_EXISTS);
                }
            }
            distributionOperatorService.updateDistributionTemplate(distributionSharePropModifyDTO);
            return ResponseWrapper.succeed(ErrorCodeEnum.SC0);
        } catch (ApiException e) {
            log.error(String.format("[后台]修改分销分润比例失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionSharePropModifyDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[后台]修改分销分润比例失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionSharePropModifyDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }


    /**
     * 分销员中心账户信息
     *
     * @param distributionUserBaseDTO
     * @return
     */
    @CurrentShop
    @RequestMapping(value = "/front/user/account/info", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[前台]获取分销员中心", response = DistributionUserAccountVO.class)
    public ResponseWrapper getDistributionUserAccount(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserBaseDTO distributionUserBaseDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.getDistributionAccount(distributionUserBaseDTO));
        } catch (ApiException e) {
            log.error(String.format("[前台]获取分销员中心失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBaseDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[前台]获取分销员中心失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBaseDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }


    /**
     * 被邀请人奖励信息
     *
     * @param distributionBaseDTO
     * @return
     */
    @CurrentShop
    @RequestMapping(value = "/front/user/incentive/info", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[前台]获取分销奖励信息", response = DistributionIncentiveVO.class)
    public ResponseWrapper getIncentiveInfo(
            @ApiParam(value = "分销商户信息") @RequestBody DistributionBaseDTO distributionBaseDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.getIncentive(distributionBaseDTO));
        } catch (ApiException e) {
            log.error(String.format("[前台]获取分销员中心失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionBaseDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[前台]获取分销员中心失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionBaseDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 查询获取收益明细
     *
     * @param distributionUserBasePageDTO
     * @return
     */
    @CurrentShop
    @RequestMapping(value = "/front/user/earnings/list", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[前台]获取收益明细", response = DistributionUserEarningsVO.class)
    public ResponseWrapper getUserEarningsList(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserBasePageDTO distributionUserBasePageDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.pageDistributionEarningsByUserId(distributionUserBasePageDTO));
        } catch (ApiException e) {
            log.error(String.format("[前台]获取收益明细失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBasePageDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[前台]获取收益明细失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBasePageDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 查询邀请用户明细
     *
     * @param distributionUserBasePageDTO
     * @return
     */
    @CurrentShop
    @RequestMapping(value = "/front/user/inviter/list", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[前台]获取邀请用户明细", response = DistributionUserInviterVO.class)
    public ResponseWrapper getUserInviterList(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserBasePageDTO distributionUserBasePageDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.pageDistributionInviterByUserId(distributionUserBasePageDTO));
        } catch (ApiException e) {
            log.error(String.format("[前台]获取邀请用户明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBasePageDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[前台]获取邀请用户明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBasePageDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 查询推广订单信息
     *
     * @param distributionUserBasePageDTO
     * @return
     */
    @CurrentShop
    @RequestMapping(value = "/front/user/order/list", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[前台]获取推广订单明细", response = DistributionUserOrderVO.class)
    public ResponseWrapper listUserOrder(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserBasePageDTO distributionUserBasePageDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.pageDistributionOrderByUserId(distributionUserBasePageDTO));
        } catch (ApiException e) {
            log.error(String.format("[前台]获取推广订单明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBasePageDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[前台]获取推广订单明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserBasePageDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 查询店铺业绩记录
     *
     * @param distributionUserPageDTO 分页查询信息
     * @return
     */
    @CurrentManager
    @RequestMapping(value = "/back/user/order/list", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[后台]获取业绩记录", response = DistributionUserOrderVO.class)
    public ResponseWrapper pageUserOrder(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserMobilePageDTO distributionUserPageDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.pageDistributionOrderByUserMobile(distributionUserPageDTO));
        } catch (ApiException e) {
            log.error(String.format("[后台]获取业绩记录明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserPageDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[后台]获取业绩记录明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserPageDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 导出店铺业绩记录
     *
     * @param distributionUserMobileDTO 查询导出信息
     * @return
     */
    @CurrentManager
    @RequestMapping(value = "/back/user/order/excel", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[后台]获取业绩记录导出", response = String.class)
    public ResponseWrapper exportUserOrder(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserMobileDTO distributionUserMobileDTO) {
        try {
            List<DistributionUserOrderVO> orderVoList = distributionQueryService.listDistributionOrderByUserMobile(distributionUserMobileDTO);
            String url = distributionQueryService.exportShopOrder(orderVoList);
            return ResponseWrapper.succeed(url);
        } catch (ApiException e) {
            log.error(String.format("[后台]获取业绩记录明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserMobileDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[后台]获取业绩记录明细，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserMobileDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 获取分销员列表
     *
     * @param distributionUserByMobilePageDTO 查询条件
     * @return
     */
    @CurrentManager
    @RequestMapping(value = "/back/user/account/list", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[后台]获取分销员列表", response = DistributionUserVO.class)
    public ResponseWrapper listUserAccount(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserByMobilePageDTO distributionUserByMobilePageDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.pageDistributionUserByMobile(distributionUserByMobilePageDTO));
        } catch (ApiException e) {
            log.error(String.format("[后台]获取分销员列表，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserByMobilePageDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[后台]获取分销员列表，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserByMobilePageDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 获取分销员列表
     *
     * @param distributionUserMobileDTO 查询条件
     * @return
     */
    @CurrentManager
    @RequestMapping(value = "/back/user/ref", method = RequestMethod.POST)
    @ApiOperation(tags = "分销", httpMethod = "POST", value = "[后台]获取分销员关系", response = DistributionUserRefVO.class)
    public ResponseWrapper listUserAccount(
            @ApiParam(value = "用户基本信息") @RequestBody DistributionUserMobileDTO distributionUserMobileDTO) {
        try {
            return ResponseWrapper.succeed(distributionQueryService.listAllDistributionUserRef(distributionUserMobileDTO));
        } catch (ApiException e) {
            log.error(String.format("[后台]获取分销员关系，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserMobileDTO), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[后台]获取分销员关系，参数:%s, 错误信息:%s", JSONObject.toJSONString(distributionUserMobileDTO), e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/firstInit", method = RequestMethod.POST)
    @ApiOperation(tags = "初始化", httpMethod = "POST", value = "[后台]初始化", response = DistributionUserRefVO.class)
    public ResponseWrapper firstInit() {
        try {
            distributionOperatorService.firstInit();
            return ResponseWrapper.succeed(ErrorCodeEnum.SC0);
        } catch (ApiException e) {
            log.error(String.format("[后台]初始化失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[后台]初始化失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
}
